package services

import (
	"context"
	"errors"
	"feng-che.com/server/driver-quiz/internal/constants"
	"feng-che.com/server/driver-quiz/internal/dep_db"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"mongoDB"
)

// Service 这个是平台提供的服务类型
type Service struct {
	mongoDB.MongoDefaultModelNonID `bson:"basic"`
	ID                             primitive.ObjectID      `bson:"_id,omitempty" json:"id"`
	CreatedBy                      primitive.ObjectID      `bson:"created_by" json:"createdBy"` // 创建人ID
	Name                           string                  `json:"name" bson:"name"`
	Description                    string                  `json:"description" bson:"description"`
	ExamCourse                     constants.ExamCourse    `json:"examCourse" bson:"examCourse"`                   // 考试科目
	InstitutionID                  primitive.ObjectID      `bson:"institution_id,omitempty" json:"institution_id"` // 模拟考场机构ID
	Nature                         constants.ServiceNature `json:"nature" bson:"nature"`                           // 服务类型,用来合适服务是否适用
	Price                          int                     `json:"price" bson:"price"`
	UnderlinePrice                 int                     `json:"underlinePrice" bson:"underline_price"` // 划线价格
	StopService                    bool                    `json:"stopService" bson:"stopService"`
	Duration                       int                     `json:"duration" bson:"duration"` // 服务时长，单位分钟
	// 分成
	ProfitSharing `json:"profitSharing" bson:"profit_sharing"`
}

type ProfitSharing struct {
	Association int `json:"association" bson:"association"` // 协会
	Platform    int `json:"platform" bson:"platform"`       // 平台
	School      int `json:"school" bson:"school"`           // 驾校
	Principal   int `json:"principal" bson:"principal"`     // 校长
	FrontDesk   int `json:"frontDesk" bson:"frontDesk"`     // 驾校前台
	Coach       int `json:"coach" bson:"coach"`             // 教练
}

func (s *Service) Save(ctx context.Context, updateOptions ...*options.UpdateOptions) (*mongo.UpdateResult, error) {
	if s.ID.IsZero() {
		s.ID = primitive.NewObjectID()
	}
	var opts []*options.UpdateOptions
	if len(updateOptions) > 0 && updateOptions[0] != nil {
		opts = updateOptions
	} else {
		opts = append(opts, options.Update().SetUpsert(true))
	}
	result, err := s.GetCollection().UpdateByID(ctx, s.ID, bson.D{
		{"$set", s},
	}, opts...)
	return result, err
}

func (s *Service) Update(ctx context.Context, dataFields *bson.D, opts ...*options.UpdateOptions) error {
	if s.ID.IsZero() {
		return errors.New("没有带入id")
	}
	*dataFields = append(*dataFields, bson.E{Key: "$set", Value: bson.D{*mongoDB.CommonUpdateTimeFieldWithCurrentTime()}})
	_, err := s.GetCollection().UpdateByID(ctx, s.ID, *dataFields, opts...)
	return err
}

func (s *Service) Delete(ctx context.Context) error {
	if s.ID.IsZero() {
		return errors.New("没有带入id")
	}
	delFiled := bson.D{}
	fields := mongoDB.CommonDeleteWriteFields()
	for _, i2 := range *fields {
		delFiled = append(delFiled, i2)
	}
	return s.Update(ctx, &bson.D{
		{"$set", delFiled},
	})
}

func (s *Service) GetCollectionName() string {
	return "service"
}

func (s *Service) GetCollection() *mongo.Collection {
	name := s.GetCollectionName()
	return dep_db.ObjectCollection(&name)
}
